//headers 
#include<algorithm>
#include<cassert>
#include<cmath>
#include<cstdio>
#include<deque>
#include<iostream>
#include<list>
#include<map>
#include<numeric>
#include<deque>
#include<set>
#include<string>
#include<sstream>
#include<stack>
#include<streambuf>
#include<vector>
using namespace std;

//macros
#define LET(x,a) typeof(a) x(a)
#define FOR(i,a,n) for(LET(i,a);i!=n;i++)
#define REP(i,n) FOR(i,0,n)
#define sz size()
#define pb push_back
#define ALL(x) x.begin(),x.end()
#define dbge(x) cout<<#x<<"->"<<x<<endl;
#define PRINT(x) REP(_,x.sz) cout<<x[_]<<" "; cout<<endl;
#define OUT(x) x<<" "
const int INF = (int) 1e8;
#define VI vector<int>
#define VS vector<string>
#define VVI vector< vector<int> >
#define LL long long
const double  pi =acos(-1.0);
//code of suren

int main(){
  int times; 

  cin>>times;
  while(times--){
    double x,y; x=y=0;
    double angle = 0;
    string s; int t; 
    int rep; cin>>rep;
    while(rep--){
      cin>>s>>t;
      if(s=="fd"){  x+=t*cos(pi * angle/180); y+=t*sin(pi* angle/180  ); } 
      else if(s=="bk") { x-=t*cos(pi * angle /180); y-=t*sin(pi*angle/180); } 
      else if(s=="lt") { angle+=t; if(angle>=360) angle-=360; }
      else if(s=="rt") { angle-=t; if(angle<=360) angle+=360; }
    }
    int ans = int( pow(x*x + y*y , 0.5) + 0.5 );
    cout<<ans<<endl;
  }
  return 0;
}
